﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO_MechSys;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace DAL_MechSys
{
    public abstract class DALBase
    {
        string connectionString;
        public DALBase()
        {
            string Computador = SystemInformation.ComputerName;

            switch (Computador)
            {
                case "PCRODRIGO": connectionString = "Data Source=PCRODRIGO\\SQLRODRIGO;Initial Catalog=MechSysDB;Integrated Security=True"; break;
               // case "NOTE-ALAN": connectionString = "Data Source=NOTE-ALAN;Initial Catalog=MechSysDB;Integrated Security=True"; break;
                default: connectionString = "Data Source=PCRODRIGO\\SQLRODRIGO;Initial Catalog=MechSysDB;user=u_gpj;password=u_gpj"; break;
            }
        }

        protected SqlDataReader ExecuteReader(string storedProssidere, params SqlParameter[] parameters)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection(connectionString);

                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                {
                    sqlCommand.CommandText = storedProssidere;

                    sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                    sqlCommand.Parameters.AddRange(parameters);

                    return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
            catch (Exception)
            {               
                throw;
            }
        }

        protected SqlDataReader ExecuteReader(string storedProssidere)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection(connectionString);

                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                {
                    sqlCommand.CommandText = storedProssidere;

                    sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                    return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
            catch (SqlException)
            {                
                throw;
            }
        }

        protected int ExecuteNonQuery(string storedProssidere, params SqlParameter[] parameters)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();

                    using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                    {
                        sqlCommand.CommandText = storedProssidere;                 
                        
                        sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                        sqlCommand.Parameters.AddRange(parameters);

                        return sqlCommand.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException)
            {               
                throw;
            }
        }

        protected int ExecuteNonQuery(string storedProssidere)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();

                    using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                    {
                        sqlCommand.CommandText = storedProssidere;

                        sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                        return sqlCommand.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
        }
    }
}